<template>
  <el-container>
  <!-- 页面头部区域 -->
  <el-header>
    <div>
      <img src="https://himg.bdimg.com/sys/portraitn/item/public.1.f6b49ac6.lsdqGGdHbQx40-WOWSahGA" alt="">
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <!-- 页面主体区域 -->
  <el-container>
    <!-- 页面侧部区域aaa -->
    <el-aside :width="isSollapse ? '64px' : '200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
       <!-- :代表unique-opened(只打开一个子菜单)属性绑定,否则true只表示一个字符串 也可以简写 例如router直接表示为true，因为默认是false -->
       <!-- router 表示开启路由模式 点到某一个子菜单 默认跳的路径为它的index :default-active :表示动态赋值-->
      <el-menu background-color="#333744" text-color="#FFF" active-text-color="BLUE"
       :unique-opened="true" :collapse="isSollapse" :collapse-transition="false" router :default-active="activePath">
        <!-- :index 属性保证该菜单是唯一的，每个菜单操作互不影响 -->
       <el-submenu :index="item.id + '' " v-for="item in menuList" :key="item.id">
          <template slot="title">
            <!-- <i class=iconObj.[125]></i>  不起作用 class=el-icon-location-->
            <i :class="iconObj[item.id]"></i>
            <span>{{item.authName}}</span>
          </template>
          <!-- 二级菜单 -->
          <!-- router 表示开启路由模式 点到某一个子菜单 默认跳的路径为它的index -->
        <el-menu-item :index="'/'+ subItem.path + '' " v-for="subItem in item.children" :key="subItem.id" @click="saveAcPath('/'+ subItem.path + '' )">
          <template slot="title">
             <i class="el-icon-menu"></i>
             <span>{{subItem.authName}}</span>
          </template>
        </el-menu-item>
        </el-submenu>
     </el-menu>
    </el-aside>
    <!-- 右侧内容主体 -->
    <el-main>
      <!-- 重定向了Welcome.vue页面 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>
<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 图标实体对象
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isSollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      //   alert('退出登录了')
      // 按要求这里的路径要填login,但是有路由导航守卫,这里随便填一个路径也没事
      this.$router.push('/login')
    },

    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.message)
      this.menuList = res.data
      // this.$message.success(this.iconObj[125])
      this.$message.success('获取菜单')
    },
    // 点击按键，切换菜单的展开与收缩
    toggleCollapse () {
      this.isSollapse = !this.isSollapse
    },
    saveAcPath (activePath) {
      this.activePath = activePath
      // 等到Home.vue被创建时,这个activePath直接被拿出来进行赋值,这一步在钩子函数里面实现
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-container{
  height: 100%;
}
.el-header{
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;//退出按钮居中
  color: white;
  font-size: 20px;
  >div{
    display: flex;
    align-items: center;
    >img{
      height: 40px;
      width: 40px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: 0px;
  }
}
.el-main{
  background-color:#eaedf1;
}
</style>
